package com.cis467.mrball.backend;


import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.cis467.mrball.parts.Button;
import com.cis467.mrball.parts.Floor;
import com.cis467.mrball.parts.Level;
import com.cis467.mrball.parts.Portal;
import com.cis467.mrball.parts.Wall;



class MyXmlContentHandler extends DefaultHandler {
	private Level xmlobj;

	private int stage =0;
	
    // implement SAX callbacks here
	public MyXmlContentHandler(){
		xmlobj = new Level();
	}	

    public void setDocumentLocator (Locator locator){
    }

    public void startDocument () throws SAXException{
    }

	public void endDocument() throws SAXException{
	}

	public void startPrefixMapping (String prefix, String uri) throws SAXException{
		
	}

	public void endPrefixMapping (String prefix) throws SAXException{
		
	}

	public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException{

		Floor start = null;
		Floor key = null;
		Floor end = null;
		
		if (qName.equals("tile"))
			buildTile(atts);
		else if(qName.equals("wall")&& stage ==0)
			buildvWall(atts);
		else if(qName.equals("hwall"))
			stage = 1;
		else if(qName.equals("wall")&& stage == 1)
			buildhWall(atts);
		else if(qName.equals("start"))
			start = makeFloor(atts);
		else if(qName.equals("end"))
			end = makeFloor(atts);
		else if(qName.equals("key"))
			key = makeFloor(atts);
//		else if(qName.equals("map"))
//			setupMap(atts);

		if(start != null)
			xmlobj.setStart(start);
		
		if(key != null)
			xmlobj.setKey(key);
		
		if(end != null)
			xmlobj.setEnd(end);
		
	}

//	private void setupMap(Attributes atts) {
//		xmlobj.setCreator(atts.getValue("creator"));
//		xmlobj.setName(atts.getValue("name"));
//	}

//	private void placeKey(Attributes atts) {
//		Floor f = new Floor(i(atts.getValue("c")), 
//				i(atts.getValue("r")),
//				i(atts.getValue("t")));
//		xmlobj.setKey(f);
//		
//	}
//
//	private void placeEnd(Attributes atts) {
//		Floor f = new Floor(i(atts.getValue("c")), 
//				i(atts.getValue("r")),
//				i(atts.getValue("t")));
//		xmlobj.setEnd(f);
//		
//	}
//
//	private void placeStart(Attributes atts) {
//		Floor f = new Floor(i(atts.getValue("c")), 
//				i(atts.getValue("r")),
//				i(atts.getValue("t")));
//		xmlobj.setStart(f);
//	}

	private void buildhWall(Attributes atts) {
		Wall w = new Wall(i(atts.getValue("c")), 
				i(atts.getValue("r")),
				i(atts.getValue("t")));
		xmlobj.setHWall(w);
		
	}

	private void buildvWall(Attributes atts) {
		
		Wall w = new Wall(i(atts.getValue("c")), 
				i(atts.getValue("r")),
				i(atts.getValue("t")));

		xmlobj.setVWall(w);		
	}

	private void buildTile(Attributes atts) {
		
		Floor tile = null;
		//if portal or button
		if(atts.getValue("color")!=null){
			if(atts.getValue("f").equals("b")){ // button
				Button b = new Button(
						i(atts.getValue("c")), 
						i(atts.getValue("r")),
						i(atts.getValue("t")),
						i(atts.getValue("color")),
						new Portal(0,0),
						new Portal(0,0));
				xmlobj.setButton(b);
				tile = b;
			}else{
				Portal p = new Portal( i(atts.getValue("c")), 
						i(atts.getValue("r")),
						i(atts.getValue("t")),
						i(atts.getValue("color")));
				xmlobj.setPortal(p);
				tile = p;
			}
		}else{//normal tile
			Floor f = makeFloor(atts);
			tile = f;					
		}
		xmlobj.setFloor(tile);
	}
	
	private Floor makeFloor(Attributes atts) {
		return new Floor( i(atts.getValue("c")),
				 i(atts.getValue("r")),
				 i(atts.getValue("t")));
	}

	public void endElement (String uri, String localName, String qName) throws SAXException{

	}

	public void characters (char ch[], int start, int length)throws SAXException {

	}

	public void ignorableWhitespace (char ch[], int start, int length)throws SAXException{
		
	}

	public void processingInstruction (String target, String data)throws SAXException{
		
	}

	public void skippedEntity (String name)	throws SAXException{
		
	}
	
	public Level getLevel(){
		return xmlobj;	
	}
	
	private int i(String s){
		return Integer.parseInt(s);
	}
	
}
